# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================




# ==================================
# In "calculating_district_dispersion.R" we calculated the dispersion
# in districts after electoral reform with only LDP, DPJ, and NFP candidates.
# saved as: "dispersion_in_districts_postER_majorcands.csv"


# ==================================
# Calculating dispersion 
# (sample: candidates from all parties presenting lists in PR after electoral reform)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

ideal.list <- ideal[ideal$year>1995 & (ideal$pty==1|
                                                           ideal$pty==52|
                                                           ideal$pty==54|
                                                           ideal$pty==5|
                                                           ideal$pty==3|
                                                           ideal$pty==71|
                                                           ideal$pty==72|
                                                           ideal$pty==92|
                                                           ideal$pty==57|
                                                           ideal$pty==91|
                                                           ideal$pty==7|
                                                           ideal$pty==58|
                                                           ideal$pty==95),]
district.var.all.list <- by(ideal.list, list(ideal.list$ku, ideal.list$year), function(x) var(x[,5]))

# We saved this as "dispersion_in_districts_allcandspostER.csv"
# and made the followning "NA":
# ku=1703 in 1996 (JCP cand was miscoded)
# ku=2811 in 1996 (no manifesto for LDP cand)
# ku=2308 in 2005 (no manifesto for NNP cand)
# Renamed district column "ku".

rm(list=ls())





# ==================================
# Calculating dispersion 
# (sample: candidates from majority-seeking parties (LDP, NFP, DPJ) plus their
# small party allies after electoral reform)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

ideal.96 <- ideal[ideal$year==1996 & (ideal$pty==1|
                                        ideal$pty==52|
                                        ideal$pty==54),]
ideal.00 <- ideal[ideal$year==2000 & (ideal$pty==1|
                                        ideal$pty==52|
                                        ideal$pty==3),]
ideal.03 <- ideal[ideal$year==2003 & (ideal$pty==1|
                                        ideal$pty==52|
                                        ideal$pty==3),]
ideal.05 <- ideal[ideal$year==2005 & (ideal$pty==1|
                                        ideal$pty==52|
                                        ideal$pty==3),]
ideal.09 <- ideal[ideal$year==2009 & (ideal$pty==1|
                                        ideal$pty==52|
                                        ideal$pty==3|
                                        ideal$pty==58|
                                        ideal$pty==91),]
maj.co <- rbind(ideal.96, ideal.00, ideal.03, ideal.05, ideal.09) 
district.var.maj.co <- by(maj.co, list(maj.co$ku, maj.co$year), function(x) var(x[,5]))

# We saved this as "dispersion_in_districts_majcocandspostER.csv"
# and made the followning "NA":
# ku=1703 in 1996 (JCP cand was miscoded)
# ku=2811 in 1996 (no manifesto for LDP cand)
# ku=2308 in 2005 (no manifesto for NNP cand)
# Renamed district column "ku".

rm(list=ls())




# ==================================
# Comparing dispersion across three categories of candidate

# Category 1: all candidates from parties presenting lists in PR

district.var.all.list <- read.csv("dispersion_in_districts_allcandspostER.csv")
colnames(district.var.all.list) <- c("ku", "1996","2000","2003", "2005", "2009")
district.var.all.list2 <- reshape(district.var.all.list,
                                  varying=c("1996", "2000", "2003", "2005", "2009"),
                                  v.names="variance",
                                  timevar="year",
                                  times=c("1996", "2000", "2003", "2005", "2009"),
                                  direction="long")
district.var.all.list2$ku_year <- paste(district.var.all.list2$ku, district.var.all.list2$year, sep="_")
district.var.all.list3 <- district.var.all.list2[complete.cases(district.var.all.list2),]
all.list.96 <- district.var.all.list3[district.var.all.list3$year=="1996",]
all.list.00 <- district.var.all.list3[district.var.all.list3$year=="2000",]
all.list.03 <- district.var.all.list3[district.var.all.list3$year=="2003",]
all.list.05 <- district.var.all.list3[district.var.all.list3$year=="2005",]
all.list.09 <- district.var.all.list3[district.var.all.list3$year=="2009",]

# Category 2: candidates of large parties

district.var.large <- read.csv("dispersion_in_districts_postER_majorcands.csv")
colnames(district.var.large) <- c("ku", "1996","2000","2003", "2005", "2009")
district.var.large2 <- reshape(district.var.large,
                              varying=c("1996", "2000", "2003", "2005", "2009"),
                              v.names="variance",
                              timevar="year",
                              times=c("1996", "2000", "2003", "2005", "2009"),
                              direction="long")
district.var.large2$ku_year <- paste(district.var.large2$ku, district.var.large2$year, sep="_")
district.var.large3 <- district.var.large2[complete.cases(district.var.large2),]
maj.96 <- district.var.large3[district.var.large3$year=="1996",]
maj.00 <- district.var.large3[district.var.large3$year=="2000",]
maj.03 <- district.var.large3[district.var.large3$year=="2003",]
maj.05 <- district.var.large3[district.var.large3$year=="2005",]
maj.09 <- district.var.large3[district.var.large3$year=="2009",]

# Category 3: large parties plus their small party allies

district.var.maj.co <- read.csv("dispersion_in_districts_majcocandspostER.csv")
colnames(district.var.maj.co) <- c("ku", "1996","2000","2003", "2005", "2009")
district.var.maj.co2 <- reshape(district.var.maj.co,
                                varying=c("1996", "2000", "2003", "2005", "2009"),
                                v.names="variance",
                                timevar="year",
                                times=c("1996", "2000", "2003", "2005", "2009"),
                                direction="long")
district.var.maj.co2$ku_year <- paste(district.var.maj.co2$ku, district.var.maj.co2$year, sep="_")
district.var.maj.co3 <- district.var.maj.co2[complete.cases(district.var.maj.co2),]
maj.co.96 <- district.var.maj.co3[district.var.maj.co3$year=="1996",]
maj.co.00 <- district.var.maj.co3[district.var.maj.co3$year=="2000",]
maj.co.03 <- district.var.maj.co3[district.var.maj.co3$year=="2003",]
maj.co.05 <- district.var.maj.co3[district.var.maj.co3$year=="2005",]
maj.co.09 <- district.var.maj.co3[district.var.maj.co3$year=="2009",]

# Is dispersion different when calculated with all candidates from parties presenting lists in PR
# than when calculated with candidates from maj-seeking parties?
# (Comparing 1st and 2nd columns of Table 1)

t.test(all.list.96$variance, maj.96$variance)
t.test(all.list.00$variance, maj.00$variance)
t.test(all.list.03$variance, maj.03$variance)
t.test(all.list.05$variance, maj.05$variance)
t.test(all.list.09$variance, maj.09$variance)

# Is district-level dispersion different when calculated with all candidates from parties presenting lists in PR
# than when calculated with candidates from maj-seeking parties plus their allies?
# (Comparing 1st and 3rd columns of Table 1)

t.test(all.list.00$variance, maj.co.00$variance)
t.test(all.list.03$variance, maj.co.03$variance)
t.test(all.list.05$variance, maj.co.05$variance)
t.test(all.list.09$variance, maj.co.09$variance)

# Is district-level dispersion the same when it is calculated with candidates from maj-seeking parties
# and when calculated using candidates from maj-seeking parties plus their small party allies?
t.test(maj.00$variance, maj.co.00$variance)
t.test(maj.03$variance, maj.co.03$variance)
t.test(maj.05$variance, maj.co.05$variance)
t.test(maj.09$variance, maj.co.09$variance)

rm(list=ls())


